<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
require_once("../conn.php");

$flashid = checkTrim($_POST['flashid']);
$flashcode = checkTrim($_POST['flashcode']);
$name = checkTrim($_POST['name']);
$url = checkTrim($_POST['url']);
$author = $_POST['author'];
$imageURL = $_POST['imageURL'];
$price = $_POST['price'];
$goodprice = $_POST['goodprice'];
$promotion = 0;
if (isset($_POST['promotion'])) {
    $promotion = 1;
}
$month = $_POST['month'];
$fp = fopen($url, "r");
if ($fp) {
    $a_array = get_headers($url, true);
    $size = round($a_array['Content-Length'] / (1024 * 1000), 3);
} else {
    echo $url . " 文件不存在！";
}

$isModify = $_POST['isModify'];
$type1 = $_POST['type1'];

if (isset($_POST['packid']) && $_POST['packid'] != "") {
    $packid = $_POST['packid'];
} else {
    $packid = -1;
}
$db = new DBC();
$is_code_exist = false;
if ($isModify == 1) {
    $sql = "UPDATE t_music SET flashcode = '" . $flashcode . "',price = " . $price . ",goodprice = " . $goodprice . ",promotion = " . $promotion . ",imageURL = '" . $imageURL . "',url='" . $url . "',month = " . $month . ",name = '" . $name . "',author = '" . $author . "', url = '" . $url . "',size='" . $size . "',type1 = '" . $type1 . "' WHERE productid = " . $flashid;
} else {
    $sql = "SELECT flashcode FROM t_music WHERE flashcode= '" . $flashcode . "'";
    $data = $db->fetchData($sql);
    if ($data != null && count($data) > 0) {
        $is_code_exist = true;
    }
    $sql = "INSERT INTO t_music(flashcode,name,author,age,price,goodprice,promotion,imageURL,url,month,size,type1,createdate) VALUES('" . $flashcode . "','" . $name . "','" . $author . "',-1," . $price . "," . $goodprice . "," . $promotion . ",'" . $imageURL . "','" . $url . "'," . $month . ",'" . $size . "','" . $type1 . "',now())";
}
if ($is_code_exist) {
    $msg = "编号 " . $flashcode . " 已经存在。";
    echo "<script language='javascript'>alert('" . $msg . "'); </script>";
    echo "<script language='javascript'>history.back();</script>";
} else {
    $transfer = "musicList.php?menu=music";
    if ($db->execute($sql)) {
        //success
        //    echo "<script language='javascript'>alert('成功'); </ script>";
        echo "<script language='javascript'>location='" . $transfer . "';</script>";
    } else {
        //failue
        echo "<script language='javascript'>alert('失败'); </script>";
        echo "<script language='javascript'>history.back();</script>";
    }
}

function checkTrim($item) {
    if (isset($item)) {
        $item = trim($item);
    } else {
        $item = "";
    }
    return $item;
}
?>
